using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using CMS.api.Entity;
using CMS.api.Model;
using CMS.api.Repository;
using CMS.api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace CMS.api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class PublicController : ControllerBase
    {

        private ICURDable<Security_ICP> _Security_ICPRepository;
        // 用于保存token的配置信息
        private TokenParameter _tokenParameter;
        // 用于读取配置文件的信息
        private readonly IConfiguration _configuration;
        private ICURDable<Slideshow> _SlideshowRepository;
        public PublicController(
            IConfiguration configuration,
            ICURDable<Security_ICP> Security_ICP,
             ICURDable<Slideshow> Slideshow
        )
        {
            _Security_ICPRepository = Security_ICP;
            _SlideshowRepository = Slideshow;
            _configuration = configuration;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }

        // 根据id删除轮播图
        [HttpDelete,Route("DeleteSlidenshowById/{id}")]
        public dynamic DeleteSlidenshowById(int id){
            var s = _SlideshowRepository.Table.Where(x=>x.Id == id).FirstOrDefault();
            s.IsDeleted = true;
            s.UpdatedTime = DateTime.Now;

            var tmp = new {
                Code = 200,
                Data = s,
                Msg = "删除成功"
            };

            return JsonHelper.Serialize(tmp);

        }

        // 查找轮播图
        [HttpGet, Route("GetSlidenshow")]
        public dynamic GetSlidenshow()
        {
            var s = _SlideshowRepository.Table.Where(x => x.IsDeleted == false).ToList();
            var tmp = new
            {
                Code = 200,
                Data = s,
                Msg = "查找到了"
            };
            return JsonHelper.Serialize(tmp);
        }


        // 查找备案信息
        [HttpGet]
        public dynamic Get()
        {
            var p = _Security_ICPRepository.Table.FirstOrDefault();

            var tmp = new
            {
                Code = 200,
                Data = p,
                Msg = "查找到了"
            };

            return JsonHelper.Serialize(tmp);
        }


        // 根据备案号Id修改备案号信息
        [HttpPut("{id}")]
        public dynamic Put(int id, NewICP model)
        {
            var p = _Security_ICPRepository.Table.Where(x => x.Id == id).FirstOrDefault();

            p.PubliceSecurity = model.PubliceSecurity;
            p.ICP = model.ICP;
            p.UpdatedTime = DateTime.Now;

            var tmp = new
            {
                Code = 200,
                Data = p,
                Msg = "修改成功"
            };

            return JsonHelper.Serialize(tmp);

        }

    }
}
